package org.example.managementsystem.controller;

import org.example.managementsystem.model.Production_Review;
import org.example.managementsystem.service.Production_ReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/production_reviews")
@CrossOrigin(origins = "*")
public class Production_ReviewController {
    private final Production_ReviewService reviewService;
    @Autowired
    public Production_ReviewController(Production_ReviewService reviewService) {
        this.reviewService = reviewService;
    }

    @GetMapping
    public ResponseEntity<List<Production_Review>> getAllReviews() {
        List<Production_Review> reviews = reviewService.getAllProduction_Reviews();
        return ResponseEntity.ok(reviews);
    }

    @GetMapping("/{submission_id}")
    public ResponseEntity<Production_Review> getReviewById(@PathVariable int submission_id) {
        Production_Review review = reviewService.getProduction_ReviewById(submission_id);
        if (review != null) {
            return ResponseEntity.ok(review);
        }
        return ResponseEntity.notFound().build();
    }

    @PostMapping
    public ResponseEntity<Map<String, Object>> addReview(@RequestBody Production_Review review) {
        Production_Review savedReview = reviewService.addProduction_Review(review);
        Map<String, Object> response = new HashMap<>();
        response.put("submission_id", savedReview.getSubmission_id());
        response.put("message", "生产审核记录添加成功");
        return ResponseEntity.status(HttpStatus.CREATED).body(response);
    }

    @PutMapping("/{submission_id}")
    public ResponseEntity<Map<String, String>> updateReview(
            @PathVariable int submission_id,
            @RequestBody Production_Review review) {
        review.setSubmission_id(submission_id);
        boolean updated = reviewService.updateProduction_Review(review);

        Map<String, String> response = new HashMap<>();
        if (updated) {
            response.put("message", "生产审核记录更新成功");
            return ResponseEntity.ok(response);
        } else {
            response.put("message", "生产审核记录更新失败，未找到指定ID的记录");
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
    }

    @DeleteMapping("/{submission_id}")
    public ResponseEntity<Map<String, String>> deleteReview(@PathVariable int submission_id) {
        boolean deleted = reviewService.deleteProduction_Review(submission_id);

        Map<String, String> response = new HashMap<>();
        if (deleted) {
            response.put("message", "生产审核记录删除成功");
            return ResponseEntity.ok(response);
        } else {
            response.put("message", "生产审核记录删除失败，未找到指定ID的记录");
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
    }
}